package com.oracle.util;

import com.oracle.pojo.Student;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.commons.lang3.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class JJWTUtilsStudent {

        /**
         * 过期时间4小时，单位毫秒
         */
        public static final long EXPIRE = 1000*60*60*4;

        /*
         * 秘钥(盐)
         * */
        public static final String SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO1122";

        /**
         * claim user - 对象 -- 脱敏处理
         * 生成token字符串的方法
         * @return token字符串
         */
        public static String getJwtToken(Student student)  {
            return Jwts.builder()
                    //JWT头信息
                    .setHeaderParam("typ", "JWT")
                    .setHeaderParam("alg", "HS2256")
                    //设置分类；设置过期时间 一个当前时间，一个加上设置的过期时间常量
                    .setSubject("orl")
                    .setIssuedAt(new Date())
//                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                    //设置token主体信息，存储用户信息
                    .claim("num", student.getStuNum())
                    .claim("id", student.getStuId())
                    .claim("name", student.getStuName())
                    .claim("tel", student.getStuTel())
                    .claim("head", student.getStuHead())
                    .claim("major",student.getStuMajor())
                    .claim("grade", student.getStuGrade())
                    .claim("headList", student.getStuHeadList())
                    .claim("state", student.getStuState())
                    .signWith(SignatureAlgorithm.HS256, SECRET)
                    .compact();
        }

        /**
         * 判断token是否存在与有效
         *
         * @param jwtToken  token串
         * @return 验证结果
         */
        public static boolean checkToken(String jwtToken){
            if (StringUtils.isBlank(jwtToken)){
                return false;
            }
            try{
                //验证token
                Jwts.parser().setSigningKey(SECRET).parseClaimsJws(jwtToken);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
            return true;
        }

        /**
         * 判断token是否存在与有效
         *
         * @param request  request
         * @return 验证结果
         */
        public static boolean checkToken(HttpServletRequest request){
            try {
                String token = request.getHeader("token");
                if (StringUtils.isBlank(token)){
                    return false;
                }
                Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
            return true;
        }

        /**
         * 解密token
         *
         * @param request request
         * @return 解密结果map
         */
        public static Student getMemberIdByJwtToken(HttpServletRequest request){
            String token = request.getHeader("token");
            return decode(token);
        }

        /**
         * 解密token
         *
         * @param token token字符串
         * @return 解密结果map
         */
        public static Student getMemberIdByJwtToken(String token){
            return decode(token);
        }

        public static Student decode(String token) {
            // 封装解密结果
            if (StringUtils.isEmpty(token)){
                return null;
            }
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);
            Claims body = claimsJws.getBody();
            String num =  (String)body.get("num");
            Integer id = Integer.valueOf(body.get("id") + "");
            String studentName = null;
            if ((!ObjectUtils.isEmpty(body.get("name")))){
                studentName = body.get("name") + "";
            }
            String studentTel = null;
            if ((!ObjectUtils.isEmpty(body.get("tel")))){
                studentTel = body.get("tel") + "";
            }
            String studentHead = null;
            if ((!ObjectUtils.isEmpty(body.get("head")))){
                studentHead = body.get("head") + "";
            }
            String studentHeadList = null;
            if ((!ObjectUtils.isEmpty(body.get("headList")))){
                studentHeadList = body.get("headList") + "";
            }
            String studentMajor = null;
            if ((!ObjectUtils.isEmpty(body.get("major")))){
                studentMajor = body.get("major") + "";
            }
            String studentGrade = null;
            if ((!ObjectUtils.isEmpty(body.get("grade")))){
                studentGrade = body.get("grade") + "";
            }
            Integer state = Integer.valueOf(body.get("state") + "");
            Student student = new Student();
            student.setStuId(id);
            student.setStuName(studentName);
            student.setStuNum(num);
            student.setStuMajor(studentMajor);
            student.setStuGrade(studentGrade);
            student.setStuHead(studentHead);
            student.setStuHeadList(studentHeadList);
            student.setStuTel(studentTel);
            student.setStuState(state);

            return student;
        }
}
